﻿<?php
	require("includes/connect.inc.php");
	
	if($_GET["button"]=="reset"){
		unset($_GET);
	}
	
	if(empty($_GET["start"]))$_GET["start"]=0;  // Current start position
	$_GET["start"]=mysql_real_escape_string($_GET["start"]);
	$per_page = 20; // Number of items to show per page
    $showeachside = 8; //  Number of items to show either side of selected page
	
	$where = "1=1";
	if(!empty($_GET["month"])){
		$_GET["month"]=mysql_real_escape_string($_GET["month"]);
		$where=$where." and month(item.pubdate)='{$_GET["month"]}'";
	}
	
	if(!empty($_GET["year"]))
	{
		$_GET["year"]=mysql_real_escape_string($_GET["year"]);
		$tmpyear=$_GET["year"]-543;
		$where=$where." and year(item.pubdate)='$tmpyear'";
	}
	
	if(!empty($_GET["channelid"])){
		$_GET["channelid"]=mysql_real_escape_string($_GET["channelid"]);
		$where=$where." and channelid='{$_GET["channelid"]}'";
	}
	
	if(!empty($_GET["status"])){
		if($_GET["status"]=="req") $where=$where." and item.isPublish='0'";
		else{
			$_GET["status"]=mysql_real_escape_string($_GET["status"]);
			$where=$where." and item.isPublish='{$_GET["status"]}'";
		}
	}
	
	$sql=" SELECT count(*)
		FROM item
		JOIN channel ON item.channelid = channel.id
		WHERE $where";
	$result=$db->sql_query($sql);
	$rows=$db->sql_fetchrow($result);
	$num=$rows[0];
	
	if($num==0)	$items[]="<tr><td>ไม่พบข้อมูล</tr>";
	else $items[]="<tr>
			<td>channel<td>title<td>last update<td>author<td>approver<td>status
			</tr>";
		$sql=" SELECT channel.title,SUBSTRING(item.title,1,25),item.pubdate,author,approver,isPublish
		FROM item
		JOIN channel ON item.channelid = channel.id
		WHERE $where 		
		ORDER BY `item`.`pubdate` DESC
		LIMIT {$_GET["start"]} , $per_page ";
		
	//echo $sql;
		
	$result=$db->sql_query($sql);
	while($rows=$db->sql_fetchrow($result)){
		switch($rows[5]){
			case -1: $status="unapproved"; break;
			case 0: $status="request for approve"; break;
			case 1: $status="approve"; break;
			default: $status="request for approve"; break;
		}
		$title=$rows[1];
		if(strlen($rows[1])>25) $title.="...";
		$approver=empty($rows[4])? "ไม่พบผู้ approve": $rows[4];
		$items[]="<tr>
			<td>{$rows[0]}<td>{$title}<td>{$rows[2]}<td>{$rows[3]}<td>{$approver}<td>$status
			</tr>";
	}
	$numrows=count($items);
	
    // $items = array("red","yellow",
                    // "pink","green",
                    // "purple","blue",
                    // "orange","opal",
                    // "ruby","brown",
                    // "bronze","copper",
                    // "ginger","tope",
                    // "orange","light blue",
                    // "light green","dark yellow",
                    // "grey","black",
                    // "white","gold",
                    // "silver");

    $thispage = $PHP_SELF ;
 //   $num = count($items); // number of items in list

    $max_pages = ceil($num / $per_page); // Number of pages
    $cur = ceil($_GET["start"] / $per_page)+1; // Current page number
?>
<style type="text/css">
<!--
.pageselected {
    color: #FF0000;
    font-weight: bold;
}
-->
</style>
<table width=100% border="0" align="center" cellpadding="0" cellspacing="0" class="PHPBODY">

<!-- /////////////////        -------->
<tr>
ค้นหา
channel 
<?php
	$sql="select id,title from channel ";
	$result=$db->sql_query($sql);
?>
	<form action="<? print("$thispage"); ?>" method="GET">
		<select name="channelid">
		<option value="">---</option>
<?php
		while($row=$db->sql_fetchrow($result)){
			echo "<option value='{$row[0]}' ";
			if($_GET["channelid"]==$row[0]) echo "selected='selected'";
			echo ">{$row[1]}</option>";
		}
?>
		</select>
	
 <br>
เดือน
		<select name="month">
			<option value="">---</option>
			<? for($i=1;$i<=12;$i++){
				echo "<option value='$i' ";
				if($_GET["month"]==$i) echo "selected='selected'";
				echo ">$i</option>";
			}
			?>
		</select>
ปี
		<select name="year">
			<option value="">---</option>
			<? for($i=2551;$i<=2600;$i++){ 
				echo "<option value='$i' ";
				if($_GET["year"]==$i) echo "selected='selected'";
				echo ">$i</option>";
			}?>
		</select>
<br>
status 
		<select name="status">
			<option value="" >---</option>
			<option value="-1" <?php if($_GET["status"]==-1) echo "selected='selected'"; ?> >unapprove</option>
			<option value="req" <?php if($_GET["status"]=='req') echo "selected='selected'"; ?> >request for approve</option>
			<option value="1" <?php if($_GET["status"]==1) echo "selected='selected'"; ?> >approve</option>
		</select>
<br>

		<input type="submit" value="Submit">
		<input type="submit" name="button" value="reset">
	</form>
</tr>

<!-- /////////////////        -------->
<tr>
<td colspan="3" align="center">
<table border='1'>
<?php
    for($x=0;$x<$numrows;$x++)print($items[$x]);
?>
</table>
</td>
</tr>

<tr><td colspan="3" align="center" valign="middle">&nbsp;</td></tr>
<tr> 
<td colspan="3" align="center" valign="middle" class="selected"> 
<?php 
$eitherside = ($showeachside * $per_page);
if($_GET["start"]+1 > $eitherside)print (" .... ");
$pg=1;
for($y=0;$y<$num;$y+=$per_page)
{
    $class=($y==$_GET["start"])?"pageselected":"";
    if(($y > ($_GET["start"] - $eitherside)) && ($y < ($_GET["start"] + $eitherside)))
    {
?>
&nbsp;<a class="<?php print($class);?>" href="<?php 
	print("$thispage".($y>0?("?start=").$y:"?start=0"));
	if(!empty($_GET["month"])||!empty($_GET["year"])) print("&month={$_GET["month"]}&year={$_GET["year"]}");
	if(!empty($_GET["channelid"])) print("&channelid={$_GET["channelid"]}");
?>"><?php print($pg);?></a>&nbsp; 
<?php
    }
    $pg++;
}
if(($_GET["start"]+$eitherside)<$num)print (" .... ");
?>
</td>
</tr>

<!-- /////////////////        -------->
<tr> 
<td width="99" align="center" valign="middle" bgcolor="#EAEAEA"> 
<?php
if(($_GET["start"]-$per_page) >= 0)
{
    $next = $_GET["start"]-$per_page;
?>
<a href="<?php
	print("$thispage".($next>0?("?start=").$next:"?start=0"));
	if(!empty($_GET["month"])||!empty($_GET["year"])) print("&month={$_GET["month"]}&year={$_GET["year"]}");
	if(!empty($_GET["channelid"])) print("&channelid={$_GET["channelid"]}");
?>"><<</a> 
<?php
}
?>
</td>
<td width="201" align="center" valign="middle" class="selected">
Page <?php print($cur);?> of <?php print($max_pages);?><br>
( <?php print($num);?> records )
</td>
<td width="100" align="center" valign="middle" bgcolor="#EAEAEA"> 
<?php 
if($_GET["start"]+$per_page<$num)
{
?>
<a href="<?php 
	print("$thispage?start=".max(0,$_GET["start"]+$per_page));
	if(!empty($_GET["month"])||!empty($_GET["year"])) print("&month={$_GET["month"]}&year={$_GET["year"]}");
	if(!empty($_GET["channelid"])) print("&channelid={$_GET["channelid"]}");
?>">&gt;&gt;</a> 
<?php
}
?>
</td>
</tr>

</table>